/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fulbitoFacil.dao;

import fulbitoFacil.dao.FulbitoFacilException;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.*;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Omar
 */

public class BaseDAO implements Serializable {

    private String username = "sa";
    private String password = "";
    private Connection connection = null;
    private PreparedStatement pstmt;
    private final static Logger logger = Logger.getLogger(BaseDAO.class.getName());

    public BaseDAO() throws FulbitoFacilException {
        try {
            Class.forName("org.hsqldb.jdbcDriver");
            connection = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/dbfulbitofacil", username, password);
        } catch (Exception e) {
            throw new FulbitoFacilException(e.getMessage());
        }
    }

    public Connection getConnection() {
        return connection;
    }

    public Integer insert(String insert, List<Object> datos) throws FulbitoFacilException {
        try {
            pstmt = getConnection().prepareStatement(insert, Statement.RETURN_GENERATED_KEYS);
            int posicion = 1;

            for (Object dato : datos) {
                if (dato instanceof Integer) {
                    pstmt.setInt(posicion, (Integer) dato);
                    posicion++;
                }
                if (dato instanceof Double) {
                    pstmt.setDouble(posicion, (Double) dato);
                    posicion++;
                }
                if (dato instanceof Date) {
                    pstmt.setDate(posicion, (Date) dato);
                    posicion++;
                }
                if (dato instanceof BigDecimal) {
                    pstmt.setBigDecimal(posicion, (BigDecimal) dato);
                    posicion++;
                }
                if (dato instanceof String) {
                    pstmt.setString(posicion, (String) dato);
                    posicion++;
                }
                if (dato instanceof Short) {
                    pstmt.setShort(posicion, (Short) dato);
                    posicion++;
                }
                if (dato instanceof Boolean) {
                    pstmt.setBoolean(posicion, (Boolean) dato);
                    posicion++;
                }
            }
            pstmt.executeUpdate();
            /*ResultSet keys = pstmt.getGeneratedKeys();
            keys.next();
            Integer llave = keys.getInt(1);
            return llave;*/
            return 0;

        } catch (Exception e) {
            //logger.log(Level.SEVERE, "Error insertando datos", ex);
            throw new FulbitoFacilException(e.getMessage());
        } finally {
            try {
                pstmt.execute("SHUTDOWN;");
                pstmt.close();
            } catch (Exception ex) {
            }
            try {
                connection.close();
            } catch (Exception ex) {
            }
        }
    }

    public ResultSet select(String select) {
        try {
            pstmt = connection.prepareStatement(select);
            return pstmt.executeQuery();
        } catch (Exception ex) {
            try {pstmt.execute("SHUTDOWN;");pstmt.close();} catch (Exception ex1) {}
            try {connection.close();} catch (Exception ex1) {}
        }
        return null;
    }

}